package com.example.framework;

public class ContactHelper extends BaseHelper{

    public ContactHelper(HelperManager manager) {
        super(manager);
    }

    public void createContact(Contact contact) {
        initContactCreation();
        fillContactForm(contact);
        confirmContactCreation();
    }

    private void initContactCreation() {
        manager.getAutoItHelper()
                .winWaitAndActivate("AddressBook Portable", "", 5000)
                .click("Add").winWaitAndActivate("Add Contact", "", 5000);
    }

    private void fillContactForm(Contact contact) {
        manager.getAutoItHelper()
                .send("TDBEdit12", contact.getFirstName())
                .send("TDBEdit11", contact.getLastName());
    }

    private void confirmContactCreation() {
        manager.getAutoItHelper()
                .click("Save")
                .winWaitAndActivate("AddressBook Portable", "", 5000);
    }

    public boolean isContactFound(Contact contact) {
        searchContact(contact);
        //wait message box that contact is not found
        return (!waitMessageDialog("Information", 3));
    }

    private void searchContact(Contact contact) {
        manager.getAutoItHelper()
                .winWaitAndActivate("AddressBook Portable", "", 5000)
                .send("TEdit1", contact.getLastName())
                .click("TRbButton10");
    }

    private boolean waitMessageDialog(String title, int timeout) {
        boolean isWinAppear = manager.getAutoItHelper().isWinAppear(title, "", timeout);

        if (isWinAppear){
            manager.getAutoItHelper().winWaitAndActivate(title, "", 5000).click("OK");
        }
        return isWinAppear;
    }
}
